//
//  UIViewController+ACJump.m
//  ACFramework
//
//  Created by caoyq on 2019/7/11.
//

#import "UIViewController+ACJump.h"
#import "NSObject+YYModel.h"

@implementation UIViewController (ACJump)

- (void)ac_pushToVCName: (NSString *)vcName
              parameter: (nullable NSDictionary *)parameter {
    [self ac_pushToVCName:vcName parameter:parameter animated:YES];
}

- (void)ac_pushToVCName: (NSString *)vcName
              parameter: (nullable NSDictionary *)parameter
               animated: (BOOL)animated {
    
    UIViewController *targetVc = nil;
    Class cls = NSClassFromString(vcName);
    
    if ([cls isSubclassOfClass:[UIViewController class]]) {
        targetVc = [[cls alloc] init];
        if (parameter) {
            //一级属性（e.g. name）
            NSMutableDictionary *valueParams = [NSMutableDictionary dictionary];
            //二级属性（e.g. viewModel.title）
            NSMutableDictionary *keyPathParams = [NSMutableDictionary dictionary];
            [parameter.allKeys enumerateObjectsUsingBlock:^(NSString * _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([key containsString:@"."]) {
                    [keyPathParams setValue:parameter[key] forKey:key];
                }else {
                    [valueParams setValue:parameter[key] forKey:key];
                }
            }];
            
            //一级属性赋值，当前key不存在则继续下一个key赋值，并不会崩溃（不能给二级属性赋值）
            if (valueParams.allKeys.count) {
                BOOL isSuccess = [targetVc yy_modelSetWithDictionary:valueParams];
                if (!isSuccess) {
                    NSLog(@"-[ERROR]-: 属性赋值失败");
                }
            }
            
            //二级属性赋值，key不存在会崩溃（kvc赋值，key不存在会崩溃）
            [keyPathParams.allKeys enumerateObjectsUsingBlock:^(NSString *  _Nonnull key, NSUInteger idx, BOOL * _Nonnull stop) {
                [targetVc setValue:keyPathParams[key] forKeyPath:key];
            }];
        }
    }
    [self.navigationController pushViewController:targetVc animated:animated];
}

- (void)ac_performCallBackWithVCNames: (NSArray<NSString *> *)vcNames parameter: (NSDictionary *)parameter {
    
    NSMutableArray *allVcs = [NSMutableArray array];
    NSInteger count = self.navigationController.childViewControllers.count;
    for (NSInteger i = count - 1; i >= 0; i--) {
        UIViewController *preVc = self.navigationController.childViewControllers[i];
        [self queryAllSubViewControllersFromViewController:preVc containerArray:allVcs];
    }
    for (UIViewController *vc in allVcs) {
        NSString *className = NSStringFromClass([vc class]);
        if ([vc respondsToSelector:@selector(receiveOperationCallBack:)] && ![self isEqual:vc] && [vcNames containsObject:className]) {
            [vc performSelector:@selector(receiveOperationCallBack:) withObject:parameter];
        }
    }
}

- (void)queryAllSubViewControllersFromViewController: (UIViewController *)viewController containerArray: (NSMutableArray *)containerArray {
    if (!containerArray) {
        containerArray = [NSMutableArray array];
    }
    if ([self isEqual:viewController]) return;
    if (![containerArray containsObject:viewController]) {
        [containerArray addObject:viewController];
    }
    NSInteger count = viewController.childViewControllers.count;
    for (NSInteger i = count - 1; i >= 0; i--) {
        UIViewController *childVc = viewController.childViewControllers[i];
        [self queryAllSubViewControllersFromViewController:childVc containerArray:containerArray];
    }
}

@end
